package com.pan.excep;


import com.pan.entity.Result;
import com.pan.excep.error.MyCutException;
import com.pan.excep.error.SeckillCouponException;
import com.pan.excep.error.ServiceException;
import com.pan.tools.ConvertObject;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result serviceHandler(ServiceException serviceException){
        Result result = (Result) ConvertObject
                .convertObject(new Result(),serviceException);
        result.setData(serviceException);
        return result;
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result runtimeExceptionHandler(RuntimeException runtimeException){
        Result result = new Result();
        result.setCode(500)
                .setMessage(runtimeException.getMessage())
                .setData(runtimeException);
        return result;
    }


    @ResponseBody
    @ExceptionHandler
    public Result MyCutExceptionHandler(MyCutException myCutException){
        Result result = new Result<>();
        result.setMessage(myCutException.getMessage())
                .setCode(myCutException.getCode())
                .setData(myCutException);
        return result;
    }



}
